home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Devices / Serial Driver Arbitration / SerialDriverArbitration.c < prev   
Encoding:
C/C++ Source or Header  |  1996-12-04  |  3.5 KB  |  143 lines  |  [TEXT/CWIE]

  1. // Comments beginning with -LR- are changes made to
  2. // accomodate new Universal Headers
  3.  
  4. // -LR-  Changed GestaltEqu.h to Gestalt.h
  5. #include <Gestalt.h>
  6. // -LR-  Changed SysEqu.h to LowMem.h
  7. #include <LowMem.h>
  8. #include <Devices.h>
  9.  
  10. #include <stdio.h>
  11.  
  12. #define gestaltArbitorAttr                 'arb '
  13. #define gestaltSerialArbitrationExists    0
  14. #define dRAMBased        0x0040
  15. #define dOpened            0x0020
  16.  
  17. // There is a bug in InterfaceLib on PowerPC machines.
  18. // Certain low memory accessors were not included in
  19. // InterfaceLib.  One of these accessors is LMGetUnitTableEntryCount
  20. // The following routine is meant as a work-around for this
  21. // problem.  You should remove this routine when Apple
  22. // fixes the missing accessors.
  23. #if defined(powerc) || defined (__powerc)
  24. pascal short LMGetUnitTableEntryCount() 
  25. {
  26.     return (*(short*) 0x01D2);
  27. }
  28. #endif
  29.  
  30. // This is a list of drivers used for test purposes in this code
  31. Str255    gDriverNames[] = 
  32.     {
  33.         "\p.NoDrvr",     // non-existant driver
  34.         "\p.AOut",         // serial port A output
  35.         "\p.BIn",         // serial port B input 
  36.         "\p.BOut",         // serial port B output
  37.         "\p.AppleCD",     // CD-ROM output
  38.         "\p.MPP",         // AppleTalk
  39.         "\p.ASYC00",    // a hard disk driver
  40.         "\p.AIn"         // serial port A input
  41.     };
  42.  
  43.  
  44. Boolean SerialArbitrationExists(void);
  45. Boolean DriverIsOpen(StringPtr driverName);
  46. Boolean CRMInstalled(void);
  47.  
  48. // Test Gestalt to see if serial arbitration exists
  49. // on this machine
  50. Boolean SerialArbitrationExists(void)
  51. {
  52.     long    response;
  53.     OSErr    err;
  54.     
  55.     err = Gestalt(gestaltArbitorAttr, &response);
  56.     if (err)
  57.         return false;
  58.     return ((response >> gestaltSerialArbitrationExists) & 1);
  59. }
  60.  
  61.  
  62. Boolean DriverIsOpen(StringPtr driverName)
  63. {
  64.     Boolean canOpen = false;
  65.     Boolean match = false;
  66.     short    index = 0;
  67.     short    count;
  68.     DCtlHandle    dceHandle;
  69.     StringPtr    namePtr;
  70.     DCtlHandle    *theUnitTable;
  71.     
  72.     // -LR- changed this to a low memory accessor function
  73.     count = LMGetUnitTableEntryCount();
  74.     
  75.     // -LR- changed this to a low memory accessor function
  76.     theUnitTable = (DCtlHandle*)LMGetUTableBase();
  77.     
  78.     while ( !match && (index < count)) {
  79.         // get handle to a device control entry
  80.         dceHandle = theUnitTable[index];
  81.         
  82.         if (dceHandle) {
  83.             if (!( (**dceHandle).dCtlFlags & dRAMBased) )
  84.                 // RAM based drivers have a handle to their driver
  85.                 namePtr = (StringPtr)(**dceHandle).dCtlDriver+18;
  86.             else
  87.                 // ROM based drivers have a pointer to their driver
  88.                 namePtr = (StringPtr) (*(DCtlPtr)dceHandle).dCtlDriver+18;
  89.  
  90.             // not case sensitive, diacritical marks count
  91.             if (RelString(driverName, namePtr, false, true) == 0) {
  92.                 match = true;
  93.                 canOpen = ((**dceHandle).dCtlFlags & dOpened) ? true : false;
  94.             }
  95.         }
  96.         index++;
  97.     }
  98.     return canOpen;
  99. }
  100.  
  101. Boolean CRMInstalled(void)
  102. {
  103.     long    response;
  104.     OSErr    err;
  105.     
  106.     err = Gestalt(gestaltCRMAttr, &response);
  107.     if (err)
  108.         return false;
  109.     return ((response >> gestaltCRMPresent) & 1);
  110. }
  111.  
  112. #define TEST_CALL
  113. main()
  114. {
  115.     OSErr    err = noErr;
  116.     SInt16    i;
  117.     SInt16    numDrivers;
  118.     
  119.     printf("Test of serial port stuff\n");
  120.     if (CRMInstalled())
  121.         printf("Communication Resource Manager is installed.\n");
  122.     if (SerialArbitrationExists())
  123.         printf("Serial Arbitration exists\n");
  124.         
  125. #ifdef TEST_CALL
  126.     numDrivers = sizeof(gDriverNames) / sizeof(Str255);
  127.     for (i = 0; i < numDrivers; i++)
  128.         printf("DriverIsOpen(%#s)\treturned %s\n", 
  129.                 gDriverNames[i], DriverIsOpen(gDriverNames[i])?"True ":"False");
  130. #endif
  131.  
  132. #ifdef TEST_OPEN
  133.     SInt16    refNum;
  134.  
  135.     if (!DriverIsOpen("\p.AIn"))
  136.         err = OpenDriver("\p.AIn", &refNum);
  137.     if (err)
  138.         printf("OpenDriver returned %d\n", err);
  139.     err = CloseDriver(refNum);
  140.     if (err)
  141.         printf("CloseDriver returned %d\n", err);
  142. #endif
  143. }